﻿using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace GameBoard
{
    public class ConeVisual : TruncatedConeVisual3D,
        IVisual, IMoveable, IActivatable, ITerrainFeature
    {
        private double _normalBaseRadius;
        bool _belongsToMoveablePicture = false;
        public bool BelongsToMoveablePicture
        {
            get { return _belongsToMoveablePicture; }
        }
        public void PopulateVisualData(BaseVisualData theData)
        {
            _belongsToMoveablePicture = theData.BelongsToMoveablePicture;
            FactoryID = theData.FactoryID;

            var coneData = theData as ConeVisualData;
            BaseRadius = coneData.BaseRadius;
            _normalBaseRadius = BaseRadius;
            Height = coneData.Height;
            Material = coneData.Material;
            Origin = coneData.Origin;
            TopRadius = coneData.TopRadius;
            IsSolid = !coneData.IsNotSolid;
            Normal = coneData.Normal;
            
        }

        

        public bool IsSolid
        {
            get;
            set;
        }

        public ModelVisual3D VisualElement
        {
            get { return this; }
        }

        void IMoveable.MoveTo(Point3D newPoint)
        {
            var moveAnimationCone =
                new Point3DAnimation(Origin, newPoint,
                    new Duration(new TimeSpan(0, 0, 0, 1)));
            AnimationClock clock2 = moveAnimationCone.CreateClock();
            ApplyAnimationClock(TruncatedConeVisual3D.OriginProperty, clock2);
        }

        public void Activate()
        {
            double radSize = 1.5;
            var activeAnimation = new DoubleAnimation(_normalBaseRadius, _normalBaseRadius*radSize,
                new Duration(new TimeSpan(0, 0, 0, 0, 800)));
            activeAnimation = new DoubleAnimation(_normalBaseRadius, _normalBaseRadius
                                                                     * radSize, new Duration(new TimeSpan(0, 0, 0, 0, 800)));
            //activeAnimation.AutoReverse = true;
            //activeAnimation.RepeatBehavior = RepeatBehavior.Forever;

            AnimationClock activeClock = activeAnimation.CreateClock();
            ApplyAnimationClock(TruncatedConeVisual3D.TopRadiusProperty, activeClock);
            ApplyAnimationClock(TruncatedConeVisual3D.BaseRadiusProperty, activeClock);
        }

        public void Deactivate()
        {
            ApplyAnimationClock(TruncatedConeVisual3D.TopRadiusProperty, null);
            ApplyAnimationClock(TruncatedConeVisual3D.BaseRadiusProperty, null);
        }


        public Guid FactoryID
        {
            get;
            set;
        }
    }
}